﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading;
using System.Threading.Tasks;

namespace Yuan.APi.Demo.Controller
{
    [Route("api/[Controller]")]
    [ApiController]
    public class IdentityController : ControllerBase
    {

        //ABP获取CurrentUser

        //private readonly AsyncLocal<ClaimsPrincipal> _currentPrincipal = new AsyncLocal<ClaimsPrincipal>();
        //public ClaimsPrincipal Principal => _currentPrincipal.Value ?? GetClaimsPrincipal();

        private readonly IHttpContextAccessor _httpContextAccessor;


        public IdentityController(IHttpContextAccessor httpContextAccessor)
        {
            _httpContextAccessor = httpContextAccessor;
        }

        [HttpGet("getUserClaims")]
        //[Authorize]
        [Authorize(Roles = "admin")]
        public IActionResult GetUserClaims()
        {
            var d = GetClaimsPrincipal();
            return new JsonResult(from c in User.Claims select new { c.Type, c.Value });
        }

        protected ClaimsPrincipal GetClaimsPrincipal()
        {
            var f = _httpContextAccessor.HttpContext?.User;


            //.Net6的集成
            return User;
        }
    }
}
